using Digitalmes.Application.Mes.Masters.Workshops;
using Digitalmes.Application.Mes.Masters.Workshops.Commands;
using Digitalmes.Application.Mes.Masters.Workshops.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Masters;

/// <summary>
/// 生产车间终结点。
/// </summary>
public sealed class WorkshopEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/master", "workshop")
            .RequireAuthorization()
            .MapGet(GetWorkshopListAsync, "list")
            .MapGet(GetWorkshopPagedListAsync, "list-page")
            .MapPost(CreateWorkshopAsync)
            .MapPut(UpdateWorkshopAsync, "{id}")
            .MapDelete(DeleteWorkshopAsync, "{id}");
    }

    [EndpointDescription("查询车间集合")]
    public async Task<IApiResult> GetWorkshopListAsync(ISender sender, [AsParameters] GetWorkshopListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询车间")]
    public async Task<IApiResult> GetWorkshopPagedListAsync(ISender sender, [AsParameters] GetWorkshopListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建车间")]
    public async Task<IApiResult> CreateWorkshopAsync(ISender sender, MdWorkshopDto dto)
    {
        var result = await sender.Send(new CreateWorkshopCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新车间")]
    public async Task<IApiResult> UpdateWorkshopAsync(ISender sender, long id, MdWorkshopDto dto)
    {
        var result = await sender.Send(new UpdateWorkshopCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除车间")]
    public async Task<IApiResult> DeleteWorkshopAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteWorkshopCommand(id));
        return result.ToApiResult();
    }
}
